<?php
# The paragraph edit handler
# $_GET["id"]		->		paragraph id

	require_once("user_session.php");

	$id = $_GET["id"];
	$template = new Template();
	$template->set_file("paragraph","template/paragraph.html");
	setTemplateWithSession($template);
	
	$note = "";
	
	if(!isset($_SESSION['userid'])||$_SESSION['userid']==""){
		# exception: not logged in!
		$note = "<script language=javascript>\n".
				"alert(\"Please login!\");\n".
				"history.go(-1);\n".
				"</script>";
		$template->set_var("show", "none");
	} else{
		$template->set_var("show", "");
		$p = new EKParagraph($id);
		if(!$p)
		{
			#exception: no such paragraph!
			$note = "<script language=javascript>\n".
				"alert(\"No such paragraph!\");\n".
				"history.go(-1);\n".
				"</script>";
			$template->set_var("show", "none");
		}else{
		
			$ownerDocId = $p->getOwnerId(); 
			$ownerDocTitle = EKDocument::getTitleById($ownerDocId);
		
			$creatorId = $p->getAutherId();
			if($creatorId==$_SESSION["userid"])
			{
				$template->set_var("creator_link", "#");
			}else{
				$template->set_var("creator_link", "user.php?userid=".$creatorId."&method=v");
			}
			$template->set_var("paragraph_content", $p->getContent());
			$template->set_var("creator",EKUser::getAttributeById($creatorId, "name"));
			$template->set_var("update_time",date("Y/m/d [H:i:s]",$p->getUpdateTime()));
			
			$template->set_var("ownerDocTitle",$ownerDocTitle);
			$template->set_var("ownerDocId",$ownerDocId);
			$template->set_var("pageTitle", "Paragraph Editing @ [". $ownerDocTitle . "] :: " . SITENAME);
			
			$template->set_var("olderPId", $id);
			
			$initRef = "";
			$refs = $p->getRefs();
			if($refs)
			{
				$i = 0;
				foreach ($refs as $ref) {
			        $initRef = $initRef."\naddRef('p_0_refs', '0');\n"
			        			."document.getElementById('p_0_ref_".$i."_edit').value='".$ref."';\n"
			        			."editSubmit('p_0_ref_".$i."');\n";
			        ++$i;
				}
				$template->set_var("initRef", $initRef);
			}else{
				$template->set_var("initRef", "");
			}
			
			$initIL = "";
			$ils = $p->getInternalLinkIds();
			if($ils)
			{
				$i = 0;
				foreach ($ils as $il) {
			        $initIL = $initIL."\naddIL('p_0_internal_links', '0');\n"
			        			."document.getElementById('p_0_internal_link_".$i."_edit').value='".$il."';\n"
			        			."editSubmit('p_0_internal_link_".$i."');\n";
			        ++$i;
				}
				$template->set_var("initIL", $initIL);
			}else{
				$template->set_var("initIL", "");
			}
			
		}
	}
	
	$template->set_var("note", $note);
	
	$template->parse("output","paragraph");
	$template->p("output");
?>